package com.crl.module.goods.model
{
	import com.crl.events.PropertyChangeEvent;
	import com.crl.module.goods.model.vo.EquipmentVO;
	import com.crl.module.goods.model.vo.ItemVO;
	import com.crl.utils.HashMap;
	
	import flash.events.Event;
	import flash.utils.Dictionary;
	
	[Event (name="change" , type="flash.events.Event")]
	
	/**
	 * 当前用户个人物品管理; 
	 * @author crlnet
	 * 
	 */	
	public class GoodsModel extends AbstractCategoryModel
	{
		/**
		 * 类型数量限制; 
		 */		
		public static const CATEGORY_LIMIT:int=10000;
		
		
		/**
		 *  着装最底层级;
		 */		
		public static const EQUIPMENT_MINLEVEL:int=10;
		
		
		/**
		 *  重新刷新的物品的方法.外置(sj,hj 两个项目从不同地方拉数据.购物npc只能调用此model);
		 */		
		public var dataProviderRemoteCallFunction:Function;
		
		/**
		 *  为何从Array改成Hashmap ,远程返回的数据,将不时会出现guid重复.(远程的操作不可信任--，只能改这个了);
		 *  已着装列表;
		 *  快捷栏列表;
		 * */
		private var _clothing:HashMap;
		private var _shortcut:HashMap;
		
		public function GoodsModel()
		{
			_clothing=new HashMap();
			_shortcut=new HashMap();
			super();
		}
		
		
		public function dispose():void{
			this.clear();
		}
		
		private static var instance:GoodsModel;
		public static function getInstance():GoodsModel{
			if(instance==null){
				instance=new GoodsModel;
			}
			return instance;
		}
		
		/**
		 * 个人物品数据源; 
		 * @param value
		 * 
		 */		
		override public function set dataProvider(value:Array):void{
			this.clear();
			fillData(value);
			super.dataProvider=value;
		}
		
		private function fillData(value:Array):void{
			for each(var item:* in value){
				if(item is Array){
					fillData(item);
					continue;
				}
				
				if(item==null)return;
				
				if(item is ItemVO==false){
					item=getItemVOByObject(item);
				}
				this.addItem(item);
			}
		}
		
		private function getItemVOByObject(value:Object):ItemVO{
			var itemvo:ItemVO;
			if(value.level>=GoodsModel.EQUIPMENT_MINLEVEL){
				itemvo=new EquipmentVO();
			}else{
				itemvo=new ItemVO();
			}
			
			
			for(var pro:String in value){
				if(itemvo.hasOwnProperty(pro)){
					itemvo[pro]=value[pro];
				}
			}
			
			return itemvo;
		}
		
		/**
		 * 是否可添加物品; 
		 * @param item
		 * @return 
		 * 
		 */		
		public function addItemable(item:ItemVO):Boolean{
			if(item==null)return false;
			var key:Object=itemKeyFunction(item);
			
			var list:Array=this.getCategory(key);
			if(list.length<CATEGORY_LIMIT){
				return true;
			}else{
				return false;
			}
		}
		
		/**
		 * 添加一件物品; 
		 * @param item
		 * @param fireEvent
		 * 
		 */		
		public function addItem(item:ItemVO,fireEvent:Boolean=false):void{
			
			if(item is EquipmentVO && item.slot>=GoodsModel.EQUIPMENT_MINLEVEL){
				this._clothing.put(item.guid,item);
			}else if(item.slot<GoodsModel.EQUIPMENT_MINLEVEL && item.slot>0){
				this._shortcut.put(item.guid,item);
			}else{
				add(item);
			}
			if(fireEvent){
				this.dispatchEvent(new Event(Event.CHANGE));
			}
		}
		
		
		/**
		 * 重写添加项,限制在limit中; 
		 * @param item
		 * 
		 */		
		override protected function add(item:*):void{
			if(this.addItemable(item)==false)return;
			
			super.add(item);
		}
		
		/**
		 * 删除一件物品; 
		 * @param item
		 * @param fireEvent
		 * 
		 */		
		public function removeItem(item:ItemVO,fireEvent:Boolean=false):int{
			var index:int=-1;
			
			if(item==null)return index
			
			
			var mapEle:Object;
			
			/**
			 * 两个Hash表删值,返回索引0;
			 * */
			if(item is EquipmentVO && item.slot>=GoodsModel.EQUIPMENT_MINLEVEL){
				mapEle=this._clothing.remove(item.guid);
				if(mapEle)index=0;
			}else if(item.slot>0 && item.slot<GoodsModel.EQUIPMENT_MINLEVEL){
				mapEle=this._shortcut.remove(item.guid);
				if(mapEle)index=0;
				
			}else{
				index=remove(item);
			}
			
			if(fireEvent){
				this.dispatchEvent(new Event(Event.CHANGE));
			}
			return index;
		}
		
		
		/**
		 * 取得着装vo; 
		 * @return 
		 * 
		 */		
		public function get clothingList():Array{
			
			return _clothing.values();
		}
		
		/**
		 * 设置当前用户着装列表; 
		 * @param value
		 * 
		 */		
		public function set clothingList(value:Array):void{
			_clothing.clear();
			for each(var item:EquipmentVO in value){
				_clothing.put(item.guid,item);
			}
		}
		
		/**
		 * 取得已装备的物品项; 
		 * @return 
		 * 
		 */		
		public function getClothingVOBySlot(slot:int):EquipmentVO{
			for each(var item:EquipmentVO in clothingList){
				if(item.slot==slot)return item;
			}
			return null;
		}
		
		/**
		 * 取得当前用户着装的项guid 列表(主要用于服务端取得技能值); 
		 * @return 
		 * 
		 */		
		public function get clothingGUIDList():Array{
			var tempList:Array=[];
			for each(var item:EquipmentVO in clothingList){
				tempList.push(item.guid);
			}
			return tempList;
		}
		
		/**
		 * 取得当前用户着装的项itemId 列表(主要为其它人同步本人形像图片); 
		 * @return 
		 * 
		 */		
		public function get clothingItemIDList():Array{
			var tempList:Array=[];
			for each(var item:Object in clothingList){
				tempList.push(item.itemId);
			}
			return tempList;
		}
		
		/**
		 * 重写如果取得项分类方式; 
		 * @param item
		 * @return 
		 * 
		 */		
		override protected function itemKeyFunction(item:*):Object{
			return (item as ItemVO).category;
		}
		
		override public function clear():void{
			super.clear();
			
			_clothing.clear();
			_shortcut.clear();
		}
		
		/**
		 * 重写取得排好序的分类; 
		 * @param key
		 * @param isSort
		 * @return 
		 * 
		 */		
		override public function getCategory(key:Object,isSort:Boolean=false):Array{
			var list:Array=[];
			//取出全部;
			if(key==-1){
				var map:Dictionary=this.getMap();
				for each(var ite:Array in map){
					list=list.concat(ite);
				}
			}else{		
				list=super.getCategory(key);
			}
			if(isSort)list.sortOn("guid");
			return list;
		}
		
		
		public function getShortcutList():Array{
			return _shortcut.values();
		}
		
		/**
		 * get itemvo by itemId;
		 * @param itemId
		 * @categoryKey categorykey;
		 * @return 
		 * 
		 */		
		public function getItemByItemID(itemID:String,categoryKey:Object=null):ItemVO{
			var item:ItemVO;
			
			/**
			* 从快捷表中查找;
			*/	
			for each(item in getShortcutList()){
				if(item.itemID !=itemID)return item;
			}
			
			if(categoryKey==null){
				/**
				* 从分类表中查找;
				*/			
				for each(var category:Array in this.map){
					for each(item in category){
						if(item.itemID !=itemID)return item;
					}
				}
				
				return null;
			}
			
			
			for each(item in this.getCategory(categoryKey)){
				if(item.itemID !=itemID)return item;
			}
			
			
			return null;
		}
		
		/**
		 * 设置道具的个数; (如果个数被设为0将从列表中删除)
		 * @param itemId
		 * @param count
		 * 
		 */		
		public function setItemCount(itemID:String,count:int):void{
			var item:ItemVO;
			
			/**
			* 从快捷表中查找;
			*/	
			for each(item in getShortcutList()){
				if(item.itemID !=itemID)continue;
				this._setItemCount(item,count);
				return;
			}
			
			/**
			* 从分类表中查找;
			*/			
			for each(var category:Array in this.map){
				for each(item in category){
					if(item.itemID !=itemID)continue;
					this._setItemCount(item,count);
					return;
				}
			}
			
		}
		
		
		private function _setItemCount(item:ItemVO,count:int):void{
			var oldValue:int=item.count
			item.count=count;
			if(item.count<=0){
				this.removeItem(item,true);
			}
			
			this.dispatchEvent(new PropertyChangeEvent(PropertyChangeEvent.PROPERTY_CHANGE,item,"count",oldValue,count));
		}
		
	}
}